package com.itheima.health.service.impl;

import com.alibaba.dubbo.config.annotation.Service;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.itheima.health.dao.CheckGroupDao;
import com.itheima.health.dao.OrderSettingDao;
import com.itheima.health.entity.PageResult;
import com.itheima.health.pojo.CheckGroup;
import com.itheima.health.pojo.OrderSetting;
import com.itheima.health.service.CheckGroupService;
import com.itheima.health.service.OrderSettingService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service(interfaceClass = OrderSettingService.class)
@Transactional
public class OrderSettingServiceImpl implements OrderSettingService {
    @Autowired
    OrderSettingDao orderSettingDao;


    @Override
    public void upload(ArrayList<OrderSetting> ordersettings) {
        for (OrderSetting ordersetting : ordersettings) {
            long count=orderSettingDao.findByOrderDate(ordersetting.getOrderDate());
            if(count>0){
                orderSettingDao.update(ordersetting);
            }else {
                orderSettingDao.add(ordersetting);
            }


        }
    }

    @Override
    public List<Map<String,Object>> showMonth(String date) {
        String first = date+"-1";
        String last = date+"-31";
        List<OrderSetting> list= orderSettingDao.find(first,last);
        ArrayList<Map<String,Object>> mapList = new ArrayList<>();
        for (OrderSetting orderSetting : list) {
            HashMap<String, Object> map = new HashMap<>();
            map.put("date",orderSetting.getOrderDate().getDate());
            map.put("number",orderSetting.getNumber());
            map.put("reservations",orderSetting.getReservations());
            mapList.add(map);
        }
        return mapList;
    }

    @Override
    public void edit(OrderSetting orderSetting) {
        long count = orderSettingDao.findByOrderDate(orderSetting.getOrderDate());
        if(count>0){
            orderSettingDao.updateNumberByOrderDate(orderSetting);

        }else {
            orderSettingDao.add(orderSetting);
        }
    }
}
